Java Technologies Checked এবং Unchecked Exceptions এর ধারণা গাইড ও নোট

424

Java তে exception handling একটি গুরুত্বপূর্ণ বিষয়, যা প্রোগ্রামটির রানটাইমে ত্রুটি বা ভুল (error) হ্যান্ডলিং করতে সহায়ক হয়। Java তে exception দুটি প্রধান শ্রেণীতে ভাগ করা হয়েছে: Checked Exceptions এবং Unchecked Exceptions

1. Checked Exceptions:

Checked Exceptions হল এমন ধরনের exceptions যেগুলি কম্পাইলার দ্বারা চেক করা হয় এবং প্রোগ্রাম চলানোর আগে এই ধরনের exceptions হ্যান্ডল করা বাধ্যতামূলক। অন্য কথায়, Checked exceptions হল এমন exceptions যেগুলি compile-time-এ চেক করা হয় এবং throws clause বা try-catch block দিয়ে সেগুলি হ্যান্ডল করতে হয়।

Checked Exceptions এর বৈশিষ্ট্য:
  • Compile-time checked: কম্পাইলার চলাকালীন এই exceptions চেক করা হয়।
  • Mandatory handling: এই ধরনের exceptions হ্যান্ডল করা বাধ্যতামূলক। যদি আপনি একটি checked exception থ্রো করেন, তবে আপনাকে সেটি try-catch block দিয়ে হ্যান্ডল করতে হবে অথবা মেথডের সিগনেচারে throws কিওয়ার্ড ব্যবহার করতে হবে।
  • এগুলি সাধারণত I/O, Database, এবং Network সম্পর্কিত operations এর সময় ঘটে।
Checked Exception এর উদাহরণ:
  1. IOException
  2. SQLException
  3. ClassNotFoundException
উদাহরণ:
import java.io.*;

public class CheckedExceptionExample {
    public static void main(String[] args) {
        try {
            FileReader file = new FileReader("nonexistentfile.txt");  // This will throw FileNotFoundException
            BufferedReader fileInput = new BufferedReader(file);
            System.out.println(fileInput.readLine());
            fileInput.close();
        } catch (IOException e) {
            System.out.println("File not found or an I/O error occurred: " + e);
        }
    }
}

এখানে FileReader এবং BufferedReader ব্যবহার করা হয়েছে, যা IOException ছুঁড়ে দিতে পারে। তাই এই exception হ্যান্ডলিং এর জন্য try-catch ব্লক ব্যবহার করা হয়েছে।

2. Unchecked Exceptions:

Unchecked Exceptions হল এমন ধরনের exceptions যেগুলি runtime এর সময় ঘটে এবং compile-time-এ সেগুলি চেক করা হয় না। এই exceptions সাধারণত RuntimeException এর সাবক্লাস হয়। এগুলি এমন situations এ ঘটে যা সাধারণত প্রোগ্রামারের ভুলের কারণে (যেমন, নাল পয়েন্টার অ্যাক্সেস, ArrayIndexOutOfBoundsException)।

Unchecked Exceptions এর বৈশিষ্ট্য:
  • Runtime-time checked: কম্পাইলারের মাধ্যমে এগুলি চেক করা হয় না।
  • Optional handling: এই ধরনের exceptions হ্যান্ডল করা বাধ্যতামূলক নয়, তবে আপনি চাইলে এগুলি try-catch ব্লক দিয়ে হ্যান্ডল করতে পারেন।
  • এগুলি সাধারণত programming errors এর কারণে ঘটে।
Unchecked Exception এর উদাহরণ:
  1. NullPointerException
  2. ArrayIndexOutOfBoundsException
  3. ArithmeticException
  4. IllegalArgumentException
উদাহরণ:
public class UncheckedExceptionExample {
    public static void main(String[] args) {
        try {
            int[] arr = new int[5];
            arr[10] = 50;  // This will throw ArrayIndexOutOfBoundsException
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Error: Array index is out of bounds: " + e);
        }
    }
}

এখানে, একটি অবৈধ array index ব্যবহার করা হয়েছে, যার ফলে ArrayIndexOutOfBoundsException ঘটেছে। এই ধরনের exception গুলি unchecked exceptions এবং কম্পাইলারে এগুলি চেক করা হয় না, তবে runtime এ সেগুলি শনাক্ত করা হয়।

Checked এবং Unchecked Exceptions এর মধ্যে পার্থক্য:

FeatureChecked ExceptionsUnchecked Exceptions
When checked?Compile-timeRuntime-time
Handling requirementMust be handled by try-catch block or declared with throwsOptional handling
Inherit fromException (not RuntimeException)RuntimeException and its subclasses
ExampleIOException, SQLException, ClassNotFoundExceptionNullPointerException, ArithmeticException, ArrayIndexOutOfBoundsException
Typical causeExternal conditions like file I/O, database issues, etc.Programming mistakes such as accessing null, illegal arguments, etc.
  • Checked Exceptions হল সেই exceptions যেগুলি compile-time এ চেক করা হয় এবং এগুলি হ্যান্ডল করা বাধ্যতামূলক।
  • Unchecked Exceptions হল সেই exceptions যেগুলি runtime এ ঘটে এবং এগুলি হ্যান্ডল করা বাধ্যতামূলক নয়, তবে এগুলি প্রোগ্রামারের ভুলের কারণে সাধারণত ঘটে।

Java exception handling সিস্টেম এই দুই ধরনের exception এর সাহায্যে প্রোগ্রামকে আরও স্থিতিশীল ও নির্ভরযোগ্য করে তোলে।

Content added By
Promotion

Are you sure to start over?

Loading...